import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/beans/video_title_bean.dart';
import 'package:hbtrans_flutter/classpage/videolist.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';

import '../apis.dart';
import '../const.dart';

class VideoLearn extends StatefulWidget {
  final status;
  final option;

  const VideoLearn({Key key, this.status, this.option}) : super(key: key);

  @override
  _VideoLearnState createState() => _VideoLearnState();
}

class _VideoLearnState extends State<VideoLearn> {
  List<Widget> tabs = List();
  List<Widget> pages = List();
  var loadingDialog;

  @override
  void initState() {
    super.initState();
    _getVideoLabelList();
  }

  @override
  Widget build(BuildContext context) {
    return tabs.length == 0
        ? Container()
        : TabBarWidget(
            initPage: 0,
            type: TabBarWidget.TOP_TAB,
            tabItems: tabs,
            tabViews: pages,
            indicatorColor: Color(0xff23AFDC),
          );
  }

  _getVideoLabelList() {
    MyDio.getInstance(url: Const.BASEURL).get(Apis.getVideoLabelList, (data) {
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }

      setState(() {
        for (var s in data) {
          tabs.add(Container(
              height: 44,
              child: Center(
                child: Text(
                  VideoTitleBean.fromJson(s).subjectName,
                  style: TextStyle(fontSize: 16),
                ),
              )));
          pages.add(VideoList(
              subjectId: VideoTitleBean.fromJson(s).subject.toString()));
        }
      });
    });
  }
}

class TabBarWidget extends StatefulWidget {
  static const int BOTTOM_TAB = 1;
  static const int TOP_TAB = 2;
  final int type;
  final String hostTitle;
  final List<Widget> tabItems;
  final List<Widget> tabViews;
  final Color backgroundColor;
  final Color indicatorColor;
  final Widget title;
  final int initPage;
  final ValueChanged<int> onPageChanged;

  TabBarWidget(
      {Key key,
      this.hostTitle,
      this.type,
      this.tabItems,
      this.tabViews,
      this.backgroundColor,
      this.indicatorColor,
      this.title,
      this.initPage,
      this.onPageChanged})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TabBarState(this.hostTitle, this.type, this.tabViews,
        this.indicatorColor, this.title, this.initPage, this.onPageChanged);
  }
}

class _TabBarState extends State<TabBarWidget>
    with SingleTickerProviderStateMixin {
  final String _hostTitle;
  final int _type;
  final int _initPage;
  final List<Widget> _tabViews;
  final Color _indicatorColor;
  final Widget _title;
  final ValueChanged<int> _onPageChanged;

  _TabBarState(this._hostTitle, this._type, this._tabViews,
      this._indicatorColor, this._title, this._initPage, this._onPageChanged)
      : super();
  TabController _tabController;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
        initialIndex: _initPage, length: widget.tabItems.length, vsync: this);
    _pageController = PageController(initialPage: _initPage, keepPage: true);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff23AFDC),
        elevation: 0,
        title: Text('在线学习'),
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 10,
          ),
          TabBar(
            tabs: widget.tabItems,
            controller: _tabController,
            indicatorColor: _indicatorColor,
            indicatorSize: TabBarIndicatorSize.label,
            labelColor: Color(0xff23AFDC),
            unselectedLabelColor: Color(0xff999999),
            onTap: (index) {
              _pageController.jumpToPage(index);
            },
          ),
          Expanded(
            child: PageView(
              controller: _pageController,
              children: _tabViews,
              onPageChanged: (index) {
                _tabController.animateTo(index);
                _onPageChanged?.call(index);
              },
            ),
          ),
        ],
      ),
    );
  }
}
